﻿// MvcRouteUnitTester
// by Lee Dumond 
// twitter.com/LeeDumond

namespace MvcRouteUnitTester.Tests
{
   using System.Web;
   using System.Web.Mvc;
   using System.Web.Routing;
   using NUnit.Framework;
   using AssertionException = MvcRouteUnitTester.AssertionException;

   [TestFixture]
   public class RouteInfoTests
   {
      [Test]
      public void CanAlterContext()
      {
         var routeCollection = new RouteCollection();
         routeCollection.MapRoute(
            "TestRoute",
            "Home/About",
            new {controller = "Home", action = "About"}
            );

         var tester = new RouteTester(routeCollection);
         RouteInfo routeInfo = tester.WithRouteInfo("Home", "About");

         HttpContextBase context = routeInfo.HttpContext;
         context.Items.Add("key", "value");


         Assert.DoesNotThrow(() => { routeInfo.ShouldGenerateUrl("/Home/About"); },
                             "Generated URL should have matched.");
         Assert.AreEqual("value", routeInfo.HttpContext.Items["key"]);
      }

      [Test]
      public void ShouldGenerateUrl_UrlCasingMismatch_ShouldThrowAssertionException()
      {
         var routeCollection = new RouteCollection();
         routeCollection.MapRoute(
            "TestRoute",
            "Home/About",
            new {controller = "Home", action = "About"}
            );

         var tester = new RouteTester(routeCollection);

         var ex =
            Assert.Throws<AssertionException>(
               () => { tester.WithRouteInfo("Home", "About").ShouldGenerateUrl("/home/about"); },
               "Provided URL and generated URL should mismatch and throw AssertionException.");

         Assert.AreEqual("URL mismatch. Expected: \"/home/about\", but was: \"/Home/About\".", ex.Message);
      }

      [Test]
      public void ShouldGenerateUrl_UrlMatch_ShouldWorkFine()
      {
         var routeCollection = new RouteCollection();
         routeCollection.MapRoute(
            "TestRoute",
            "Home/About",
            new {controller = "Home", action = "About"}
            );

         var tester = new RouteTester(routeCollection);

         Assert.DoesNotThrow(() => { tester.WithRouteInfo("Home", "About").ShouldGenerateUrl("/Home/About"); },
                             "Generated URL should have matched.");
      }

      [Test]
      public void ShouldGenerateUrl_UrlMismatch_ShouldThrowAssertionException()
      {
         var routeCollection = new RouteCollection();
         routeCollection.MapRoute(
            "TestRoute",
            "Home/About",
            new {controller = "Home", action = "About"}
            );

         var tester = new RouteTester(routeCollection);

         var ex =
            Assert.Throws<AssertionException>(
               () => { tester.WithRouteInfo("Home", "About").ShouldGenerateUrl("/Home/ForceFail"); },
               "Provided URL and generated URL should mismatch and throw AssertionException.");

         Assert.AreEqual("URL mismatch. Expected: \"/Home/ForceFail\", but was: \"/Home/About\".", ex.Message);
      }
   }
}